package com.panshi.util;

import com.panshi.result.R;
import com.panshi.service.SendSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserRegSendSmsUtils {

    @Autowired
    private SendSmsService sendSmsService;

    @Autowired
    private RedisUtil redisUtil;

    public synchronized R regSendSms(String phone,String form){
        Object o = redisUtil.get(phone+"_"+form);
        R r = new R();
        if(o!=null && (o+"").length() == 6){
            r.setCode(202);
            r.setSuccess(true);
            r.setMessage("验证码未失效");
            return r;
        }

        String nonce_str = DxyzmUtil.getNonce_str(6);
        sendSmsService.sendsms(nonce_str,phone);
        redisUtil.set(phone+"_"+form,nonce_str,90);   //值保存90秒
        r.setCode(201);
        r.setSuccess(true);
        r.setMessage("发送成功");
        return r;
    }

}
